<?php

namespace App\Notifications;

use App\Models\Comment;
use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class GetNewReplyed extends Notification
{
    use Queueable;

    protected $comment;
    protected $parent;

    /**
     * @param mixed $parent
     */
    public function setParent($parent)
    {
        if (! $parent){
            return;
        }

        if ($parent instanceof Post){

            $this->parent = [
                'type' => 'post',
                'id' => $parent->id,
                'title' => $parent->title,
                'content' => mb_substr($parent->content,0,150)
            ];

        }

        if ($parent instanceof Comment){

            $this->parent = [
                'type' => 'comment',
                'id' => $parent->id,
                'content' => $parent->content,
                'image_url' => $parent->image_url
            ];
        }

    }
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Comment $comment,$parent)
    {
        $this->comment = $comment;
        $this->setParent($parent);
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }


    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        $fromUser = $this->comment->user;



        return [
            'from_id' => $fromUser->id,
            'from_type' => 1, // 0 系统 1 用户
            'title' => $fromUser->nickname.' 回复了你',
            'content' => $this->comment->content,
            'parent' => $this->parent
        ];

    }
}
